import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/book/BookFolders.dart';
import 'package:ichinesereader/widgets/dialogs/addToFolderDialog.dart';
import 'package:ichinesereader/widgets/dialogs/newOrEditFolderDialog.dart';
import 'package:ichinesereader/widgets/dialogs/deleteFolderDialog.dart';
import 'package:ichinesereader/widgets/dialogs/removeFromFolderDialog.dart';
import 'package:ichinesereader/widgets/dialogs/simpleAlertDialog.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:get/get.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:ichinesereader/widgets/dialogs/addToPlaylistDialog.dart';
import 'package:ichinesereader/controller/authentication.dart';
import 'package:ichinesereader/utils/log.dart';
import '../../themes/appTheme.dart';

class FavoriteButtonBar extends StatefulWidget {
  FavoriteButtonBar({
    Key? key,
  }) : super(key: key);

  @override
  State<FavoriteButtonBar> createState() => _FavoriteButtonBarState();
}

class _FavoriteButtonBarState extends State<FavoriteButtonBar> {
  final BooksController _booksController = Get.find();
  final LoginController _logincontrol = Get.find();
  final box = Hive.box('appconfig');
  String? _folderId;

  @override
  void initState() {
    _folderId = 'ROOTFOLDER';
    var isParent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Parent;
    Future.delayed((Duration(milliseconds: 100)), () {
      _booksController.setCurrentFileId('ROOTFOLDER');

      if (isParent) {
        var currentchild = box.get(CacheManagerKey['currentchild'].toString());
        _booksController
            .fetchBookAllFoldersForChild(currentchild)
            .then((folders) {
          if (folders.isNotEmpty) {
            if (_booksController.currentBookFolder.value.id != null &&
                folders.length > 0) {
              int index = folders.indexWhere((element) =>
                  element.id == _booksController.currentBookFolder.value.id);
              if (index != -1) {
                _booksController.fetchChildFavoriteReadingItemsByFolder(
                    _booksController.currentBookFolder.value.id.toString(),
                    currentchild);
              } else {
                _booksController.setCurrentBookFolder(folders[0]);
              }
              _folderId =
                  _booksController.currentBookFolder.value.id.toString();
            } else if (_booksController.currentBookFolder.value.id == null &&
                folders.length > 0) {
              _booksController.setCurrentBookFolder(folders[0]);
            } else {
              if (folders.isNotEmpty) {
                _booksController.setCurrentBookFolder(folders[0]);
              }
            }
            _folderId = _booksController.currentBookFolder.value.id.toString();
          }
        });
      } else {
        _booksController.fetchBookAllFolders().then((folders) {
          if (folders.isNotEmpty) {
            if (_booksController.currentBookFolder.value.id != null &&
                folders.length > 0) {
              int index = folders.indexWhere((element) =>
                  element.id == _booksController.currentBookFolder.value.id);
              if (index != -1) {
                _booksController.fetchMyFavoriteReadingItemsByFolder(
                    _booksController.currentBookFolder.value.id.toString());
              } else {
                _booksController.setCurrentBookFolder(folders[0]);
                _booksController
                    .fetchMyFavoriteReadingItemsByFolder('ROOTFOLDER');
              }
              _folderId =
                  _booksController.currentBookFolder.value.id.toString();
            } else if (_booksController.currentBookFolder.value.id == null &&
                folders.length > 0) {
              _booksController.setCurrentBookFolder(folders[0]);
              _booksController
                  .fetchMyFavoriteReadingItemsByFolder('ROOTFOLDER');
            } else {
              if (folders.isNotEmpty) {
                _booksController.setCurrentBookFolder(folders[0]);
              }
              _booksController
                  .fetchMyFavoriteReadingItemsByFolder('ROOTFOLDER');
            }
            _folderId = _booksController.currentBookFolder.value.id.toString();
          }
        });
      }
    });

    super.initState();
  }

  @override
  void dispose() {
    _booksController.myFavoriteBookItems.clear();
    var isParent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Parent;
    if (!isParent) {
      _booksController.selectedFavoriteReadingBookItems.clear();
    }

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('Build my favorite button bar');
    var isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    var isParent = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Parent;
    List<String> bookIds = [];
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: isSmallScreen
          ? Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Obx(() {
                  if (_booksController.bookFolders.length > 0 &&
                      _booksController.currentBookFolder.value.id != null) {
                    int index = _booksController.bookFolders.indexWhere(
                        (element) =>
                            element.id ==
                            _booksController.currentBookFolder.value.id);

                    return Container(
                      width: double.infinity,
                      decoration: BoxDecoration(
                          color: Get.isDarkMode
                              ? AppTheme.colors.darkTheme2
                              : Colors.white.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(3.0)),
                      child: new DropdownButtonFormField(
                        dropdownColor: Get.isDarkMode
                            ? AppTheme.colors.darkTheme2
                            : Get.theme.primaryColor,
                        isExpanded: true,
                        value: index != -1
                            ? _booksController.bookFolders[index]
                            : _booksController.bookFolders[0],
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.only(left: 15.0, top: 6),
                          labelStyle: TextStyle(
                              fontSize: 15.0,
                              letterSpacing: 1.0,
                              color: Get.isDarkMode
                                  ? Get.theme.primaryColorLight
                                  : Colors.black),
                          labelText: isTeacher
                              ? 'Choose Lesson Plan'.tr
                              : 'Choose Folder'.tr,
                        ),
                        validator: FormBuilderValidators.compose(
                            [FormBuilderValidators.required()]),
                        items: _booksController.bookFolders
                            .map((folder) => DropdownMenuItem(
                                  value: folder,
                                  child: Container(
                                    child: Text(
                                      '${folder.folderName}' +
                                          ' - ' +
                                          '${folder.bookCount}',
                                      style: TextStyle(
                                          fontSize: 14.0,
                                          color: Get.isDarkMode
                                              ? Get.theme.primaryColorDark
                                              : Colors.white),
                                      overflow: TextOverflow.fade,
                                      softWrap: true,
                                    ),
                                  ),
                                ))
                            .toList(),
                        onChanged: (value) {
                          var folder = value as AllFolder;
                          if (folder.id != null) {
                            _booksController.setCurrentBookFolder(folder);
                            _booksController.myFavoriteBookItems.clear();

                            if (isParent) {
                              var currentchild = box.get(
                                  CacheManagerKey['currentchild'].toString());
                              if (folder.id != null &&
                                  folder.id != 'ROOTFOLDER') {
                                _booksController
                                    .fetchChildFavoriteReadingItemsByFolder(
                                        folder.id.toString(), currentchild);
                                _booksController
                                    .setCurrentFileId(folder.id.toString());
                              } else {
                                _booksController.setCurrentFileId('ROOTFOLDER');
                              }
                            } else {
                              _booksController.selectedFavoriteReadingBookItems
                                  .clear();
                              if (folder.id != null &&
                                  folder.id != 'ROOTFOLDER') {
                                _booksController
                                    .fetchMyFavoriteReadingItemsByFolder(
                                        folder.id.toString());
                                _booksController
                                    .setCurrentFileId(folder.id.toString());
                              } else {
                                _booksController
                                    .fetchMyFavoriteReadingItemsByFolder(
                                        'ROOTFOLDER');
                                _booksController.setCurrentFileId('ROOTFOLDER');
                              }
                            }
                            _folderId = folder.id;
                          }
                        },
                      ),
                    );
                  } else {
                    return Text('');
                  }
                }),

                /// 这个空的text控件只是为了实时获取选中书的列表用
                if (!isParent)
                  Obx(() {
                    bookIds.clear();
                    if (_booksController
                            .selectedFavoriteReadingBookItems.length >
                        0) {
                      _booksController.selectedFavoriteReadingBookItems
                          .forEach((element) {
                        if (!bookIds.contains(element.bookId.toString())) {
                          bookIds.add(element.bookId.toString());
                        }
                      });
                      return Text('');
                    } else {
                      return Text('');
                    }
                  }),
                if (!isParent)
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            showGeneralDialog(
                                context: context,
                                transitionDuration: Duration(milliseconds: 400),
                                pageBuilder: (bc, ania, anis) {
                                  return NewOrEditFolderDialog(
                                      title: isTeacher
                                          ? 'Create Lesson Plan'.tr
                                          : 'Create Folder'.tr,
                                      message: '',
                                      type: 'create');
                                });
                          },
                          icon: Icon(
                            Icons.create_new_folder_outlined,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          tooltip: isTeacher
                              ? 'Create Lesson Plan'.tr
                              : 'Create Folder'.tr,
                          size: GFSize.SMALL,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            if (_folderId == 'ROOTFOLDER' ||
                                _folderId == null) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Main Folder can not be renamed!'
                                                .tr);
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return NewOrEditFolderDialog(
                                        title: isTeacher
                                            ? 'Edit Lesson Plan Name'.tr
                                            : 'Edit Folder Name'.tr,
                                        message: '',
                                        type: 'edit');
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Edit Lesson Plan Name'.tr
                              : 'Edit Folder Name'.tr,
                          icon: Icon(
                            Icons.create_sharp,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            if (_folderId == 'ROOTFOLDER' ||
                                _folderId == null) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Main Folder can not be deleted!'
                                                .tr);
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return DeleteFolderDialog(
                                        title: '',
                                        message:
                                            'Are you sure you want to '.tr);
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Delete Lesson Plan'
                              : 'Delete Folder'.tr,
                          icon: Icon(
                            Icons.delete_outlined,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            Log.debug(bookIds.length);
                            if (bookIds.length > 0) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return AddToFolderDialog(
                                        readingType:
                                            NanhaiBookReadingTypes.favorite,
                                        title: 'Move to Folder'.tr,
                                        selected: bookIds,
                                        message: '',
                                        readingTypeFrom: '');
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Please select at least one book to ADD!'
                                                .tr);
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Move to Lesson Plan'.tr
                              : 'Move to Folder'.tr,
                          icon: Icon(
                            Icons.my_library_add_outlined,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      ),
                      if (_logincontrol.getRole() ==
                              Constants.NanhaiRole_Student ||
                          _logincontrol.getRole() ==
                              Constants.NanhaiRole_Individual)
                        Padding(
                          padding: const EdgeInsets.only(right: 6.0),
                          child: GFIconButton(
                            color: Theme.of(context).primaryColorDark,
                            onPressed: () {
                              Log.debug(bookIds.length);
                              if (bookIds.length > 0) {
                                showGeneralDialog(
                                    context: context,
                                    transitionDuration:
                                        Duration(milliseconds: 400),
                                    pageBuilder: (bc, ania, anis) {
                                      return AddToPlaylistDialog(
                                          title: '',
                                          selected: bookIds,
                                          message:
                                              'Add Books to Audio Playlist'.tr,
                                          readingTypeFrom: 'favorite');
                                    });
                              } else {
                                showGeneralDialog(
                                    context: context,
                                    transitionDuration:
                                        Duration(milliseconds: 400),
                                    pageBuilder: (bc, ania, anis) {
                                      return SimpleAlertDialog(
                                          title: '',
                                          message:
                                              'Please select at least one book to ADD!'
                                                  .tr);
                                    });
                              }
                            },
                            tooltip: 'Add Books to Audio Playlist'.tr,
                            icon: Icon(
                              Icons.audiotrack,
                              color: Get.isDarkMode
                                  ? Colors.black
                                  : Theme.of(context).primaryColorLight,
                            ),
                            size: GFSize.SMALL,
                          ),
                        ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            Log.debug(bookIds.length);
                            if (bookIds.length > 0) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return RemoveFromFolderDialog(
                                        title: '',
                                        selected: bookIds,
                                        message: isTeacher
                                            ? 'Are you sure you want to REMOVE book(s) from this folder?'
                                                .tr
                                            : 'Are you sure you want to REMOVE book(s) from this folder?'
                                                .tr);
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Please select books to REMOVE!'
                                                .tr);
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Remove Books from Lesson Plan'.tr
                              : 'Remove Books from Folder'.tr,
                          icon: Icon(
                            Icons.remove_circle_outline,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      )
                    ],
                  )
              ],
            )
          : Row(
              children: <Widget>[
                Obx(() {
                  if (_booksController.bookFolders.length > 0 &&
                      _booksController.currentBookFolder.value.id != null) {
                    int index = _booksController.bookFolders.indexWhere(
                        (element) =>
                            element.id ==
                            _booksController.currentBookFolder.value.id);

                    return Container(
                      width: 300,
                      decoration: BoxDecoration(
                          color: Get.isDarkMode
                              ? AppTheme.colors.darkTheme2
                              : Colors.white.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(3.0)),
                      child: new DropdownButtonFormField(
                        dropdownColor: Get.isDarkMode
                            ? AppTheme.colors.darkTheme2
                            : Get.theme.primaryColor,
                        isExpanded: true,
                        value: index != -1
                            ? _booksController.bookFolders[index]
                            : _booksController.bookFolders[0],
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.only(left: 15.0, top: 6),
                          labelStyle: TextStyle(
                              fontSize: 15.0,
                              letterSpacing: 1.0,
                              color: Get.isDarkMode
                                  ? Get.theme.primaryColorLight
                                  : Colors.black),
                          labelText: isTeacher
                              ? 'Choose Lesson Plan'.tr
                              : 'Choose Folder'.tr,
                        ),
                        validator: FormBuilderValidators.compose(
                            [FormBuilderValidators.required()]),
                        items: _booksController.bookFolders
                            .map((folder) => DropdownMenuItem(
                                  value: folder,
                                  child: Container(
                                    child: Text(
                                      '${folder.folderName}' +
                                          ' - ' +
                                          '${folder.bookCount}',
                                      style: TextStyle(
                                          fontSize: 15.0,
                                          color: Get.isDarkMode
                                              ? Get.theme.primaryColorDark
                                              : Colors.white),
                                      overflow: TextOverflow.fade,
                                      softWrap: true,
                                    ),
                                  ),
                                ))
                            .toList(),
                        onChanged: (value) {
                          var folder = value as AllFolder;
                          if (folder.id != null) {
                            _booksController.setCurrentBookFolder(folder);
                            _booksController.myFavoriteBookItems.clear();

                            if (isParent) {
                              var currentchild = box.get(
                                  CacheManagerKey['currentchild'].toString());
                              if (folder.id != null &&
                                  folder.id != 'ROOTFOLDER') {
                                _booksController
                                    .fetchChildFavoriteReadingItemsByFolder(
                                        folder.id.toString(), currentchild);
                                _booksController
                                    .setCurrentFileId(folder.id.toString());
                              } else {
                                _booksController.setCurrentFileId('ROOTFOLDER');
                              }
                            } else {
                              _booksController.selectedFavoriteReadingBookItems
                                  .clear();
                              if (folder.id != null &&
                                  folder.id != 'ROOTFOLDER') {
                                _booksController
                                    .fetchMyFavoriteReadingItemsByFolder(
                                        folder.id.toString());
                                _booksController
                                    .setCurrentFileId(folder.id.toString());
                              } else {
                                _booksController
                                    .fetchMyFavoriteReadingItemsByFolder(
                                        'ROOTFOLDER');
                                _booksController.setCurrentFileId('ROOTFOLDER');
                              }
                            }
                            _folderId = folder.id;
                          }
                        },
                      ),
                    );
                  } else {
                    return Text('');
                  }
                }),

                /// 这个空的text控件只是为了实时获取选中书的列表用
                if (!isParent)
                  Expanded(
                    child: Obx(() {
                      bookIds.clear();
                      if (_booksController
                              .selectedFavoriteReadingBookItems.length >
                          0) {
                        _booksController.selectedFavoriteReadingBookItems
                            .forEach((element) {
                          if (!bookIds.contains(element.bookId.toString())) {
                            bookIds.add(element.bookId.toString());
                          }
                        });
                        return Text('');
                      } else {
                        return Text('');
                      }
                    }),
                  ),
                if (!isParent)
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            showGeneralDialog(
                                context: context,
                                transitionDuration: Duration(milliseconds: 400),
                                pageBuilder: (bc, ania, anis) {
                                  return NewOrEditFolderDialog(
                                      title: isTeacher
                                          ? 'Create Lesson Plan'.tr
                                          : 'Create Folder'.tr,
                                      message: '',
                                      type: 'create');
                                });
                          },
                          icon: Icon(
                            Icons.create_new_folder_outlined,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          tooltip: isTeacher
                              ? 'Create Lesson Plan'.tr
                              : 'Create Folder'.tr,
                          size: GFSize.SMALL,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            if (_folderId == 'ROOTFOLDER' ||
                                _folderId == null) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Main Folder can not be renamed!'
                                                .tr);
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return NewOrEditFolderDialog(
                                        title: isTeacher
                                            ? 'Edit Lesson Plan Name'.tr
                                            : 'Edit Folder Name'.tr,
                                        message: '',
                                        type: 'edit');
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Edit Lesson Plan Name'.tr
                              : 'Edit Folder Name'.tr,
                          icon: Icon(
                            Icons.create_sharp,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            if (_folderId == 'ROOTFOLDER' ||
                                _folderId == null) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Main Folder can not be deleted!'
                                                .tr);
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return DeleteFolderDialog(
                                        title: '',
                                        message:
                                            'Are you sure you want to '.tr);
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Delete Lesson Plan'
                              : 'Delete Folder'.tr,
                          icon: Icon(
                            Icons.delete_outlined,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            Log.debug(bookIds.length);
                            if (bookIds.length > 0) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return AddToFolderDialog(
                                        readingType:
                                            NanhaiBookReadingTypes.favorite,
                                        title: 'Move to Folder'.tr,
                                        selected: bookIds,
                                        message: '',
                                        readingTypeFrom: '');
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Please select at least one book to ADD!'
                                                .tr);
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Move to Lesson Plan'.tr
                              : 'Move to Folder'.tr,
                          icon: Icon(
                            Icons.my_library_add_outlined,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      ),
                      if (_logincontrol.getRole() ==
                              Constants.NanhaiRole_Student ||
                          _logincontrol.getRole() ==
                              Constants.NanhaiRole_Individual)
                        Padding(
                          padding: const EdgeInsets.only(right: 6.0),
                          child: GFIconButton(
                            color: Theme.of(context).primaryColorDark,
                            onPressed: () {
                              Log.debug(bookIds.length);
                              if (bookIds.length > 0) {
                                showGeneralDialog(
                                    context: context,
                                    transitionDuration:
                                        Duration(milliseconds: 400),
                                    pageBuilder: (bc, ania, anis) {
                                      return AddToPlaylistDialog(
                                          title: '',
                                          selected: bookIds,
                                          message:
                                              'Add Books to Audio Playlist'.tr,
                                          readingTypeFrom: 'favorite');
                                    });
                              } else {
                                showGeneralDialog(
                                    context: context,
                                    transitionDuration:
                                        Duration(milliseconds: 400),
                                    pageBuilder: (bc, ania, anis) {
                                      return SimpleAlertDialog(
                                          title: '',
                                          message:
                                              'Please select at least one book to ADD!'
                                                  .tr);
                                    });
                              }
                            },
                            tooltip: 'Add Books to Audio Playlist'.tr,
                            icon: Icon(
                              Icons.audiotrack,
                              color: Get.isDarkMode
                                  ? Colors.black
                                  : Theme.of(context).primaryColorLight,
                            ),
                            size: GFSize.SMALL,
                          ),
                        ),
                      Padding(
                        padding: const EdgeInsets.only(right: 6.0),
                        child: GFIconButton(
                          color: Theme.of(context).primaryColorDark,
                          onPressed: () {
                            Log.debug(bookIds.length);
                            if (bookIds.length > 0) {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return RemoveFromFolderDialog(
                                        title: '',
                                        selected: bookIds,
                                        message: isTeacher
                                            ? 'Are you sure you want to REMOVE book(s) from this folder?'
                                                .tr
                                            : 'Are you sure you want to REMOVE book(s) from this folder?'
                                                .tr);
                                  });
                            } else {
                              showGeneralDialog(
                                  context: context,
                                  transitionDuration:
                                      Duration(milliseconds: 400),
                                  pageBuilder: (bc, ania, anis) {
                                    return SimpleAlertDialog(
                                        title: '',
                                        message:
                                            'Please select books to REMOVE!'
                                                .tr);
                                  });
                            }
                          },
                          tooltip: isTeacher
                              ? 'Remove Books from Lesson Plan'.tr
                              : 'Remove Books from Folder'.tr,
                          icon: Icon(
                            Icons.remove_circle_outline,
                            color: Get.isDarkMode
                                ? Colors.black
                                : Theme.of(context).primaryColorLight,
                          ),
                          size: GFSize.SMALL,
                        ),
                      )
                    ],
                  )
              ],
            ),
    );
  }
}
